package game.fury;

import java.util.ArrayList;

import android.graphics.Paint;

import sheep.collision.CollisionLayer;
import sheep.game.Sprite;
import sheep.graphics.Font;
import sheep.gui.TextButton;
import sheep.math.Vector2;
import game.components.Balls;
import game.components.Brick;
import game.components.Constants;
import game.components.Padle;
import game.components.PowerUp;
import game.components.Score;

/**
 *	This makes all of the game components and generates the brick field 
 * @author Marius Haugen
 */
public class LoadGame {
	
	private static float offsetBrickX = 75;
	private static float offsetBrickY = 30;
	private static float offsetTop = 55;
	private static float offsetLeft = 55;
	private static float numRows = 6;
	

	
	public LoadGame(Constants con){

	}
	
	/**
	 * Sets up all of the components of the game
	 * @param furyScreen The game object
	 * @param score 
	 */
	public static void loadGame(FuryScreen furyScreen, int score){
		
		
		//div booleans
		furyScreen.gameOver = false;
		furyScreen.gameWon = false;
		furyScreen.firstTouch = false;
		furyScreen.isRunning = true;
		
		//Arraylists
		furyScreen.balls = new ArrayList<Balls>();	
		furyScreen.bricks = new ArrayList<Brick>();
		furyScreen.powerUps = new ArrayList<PowerUp>();
		furyScreen.colLayer = new CollisionLayer();
		furyScreen.currentBallSpeed = new ArrayList<Vector2>();
		
		furyScreen.con = new Constants();
		furyScreen.stats = new Score(score);
		
		// sets up the padle
		furyScreen.setPadle(new Padle(furyScreen.con.padleimage_std, furyScreen.con.padlepos, furyScreen.con.padleSizeDefault));
		furyScreen.colLayer.addSprite(furyScreen.padle);
		
		//sets up sprites on the FuryScreen
		furyScreen.backgroudSprite = new Sprite(furyScreen.con.furyBackground);
		furyScreen.backgroudSprite.setPosition(240,200);
		
		furyScreen.playLine = new Sprite(furyScreen.con.playLine);
		furyScreen.playLine.setPosition(240,750);
		
		furyScreen.scoreLine = new Sprite(furyScreen.con.scoreLine);
		furyScreen.scoreLine.setPosition(240, 10);
		
		furyScreen.pauseButton = new TextButton(400, 25, "Pause", new Paint[] {Font.WHITE_SANS_BOLD_20, Font.WHITE_SANS_BOLD_20});
		

		
		//generate field
		if(furyScreen.getType() == 0){
			layBricksRandom(furyScreen.con, furyScreen.bricks);
		}
		if(furyScreen.getType() == 1){
			diamondShapeBrick(furyScreen.con, furyScreen.bricks);
		}
		if(furyScreen.getType() == 2){
			layBricksFull(furyScreen.con, furyScreen.bricks);
		}
		if(furyScreen.getType() == 3){
			level3Brick(furyScreen.con, furyScreen.bricks);
		}
		
		// add all bricks to the collisionsLayer
		for(int i = 0; i<furyScreen.bricks.size(); i++ ){
			furyScreen.colLayer.addSprite(furyScreen.bricks.get(i));
		}
		
		//add ball on top of the padle
		Balls ball = new Balls(furyScreen.con.ballimage,new Vector2(0, 0), new Vector2(240, 675), 1);
		furyScreen.balls.add(ball);
		
		// add all balls to the collisionsLayer and adds listners
		for(int i = 0; i<furyScreen.balls.size(); i++ ){
			furyScreen.addToColLayer(furyScreen.balls.get(i));
		}
		

	}
	
	/**
	 * Lays bricks random on the playfield
	 * @param con Constants needed for the brick generating
	 * @param bricks Arraylist that will contain all of the bricks generated 
	 * @author Erlend Heimark
	 */
	private static void layBricksRandom(Constants con, ArrayList<Brick> bricks){
		numRows = 8;
		for(int i = 0; i<6; i++){
			float offsetx = offsetLeft+(offsetBrickX*i);
			for(int j = 0; j<numRows; j++){
				float offsety = offsetTop+(offsetBrickY*j);
				if(Math.random()<con.brickFreq){
					double random = Math.random();
					if(random<0.33f){
						bricks.add(new Brick(con.brickimageGreen,new Vector2(offsetx, offsety), con.brickGreenStrength, con.brickFreq));
					}
					else if(random>0.33f & random<0.66f){
						bricks.add(new Brick(con.brickimageBlue,new Vector2(offsetx, offsety), con.brickBlueStrength, con.brickFreq));
					}
					else if(random>0.66f){
						bricks.add(new Brick(con.brickimageRed,new Vector2(offsetx, offsety), con.brickRedStrength, con.brickFreq));
					}
					
				
				}
			}
		}
	}
	

	/**
	 * Makes a solid 6x numRows brick field
	 * @param con Constants needed for the brick generating
	 * @param bricks Arraylist that will contain all of the bricks generated 
	 */
	private static void layBricksFull(Constants con, ArrayList<Brick> bricks){
		for(int i = 0; i<6; i++){
			float offsetx = offsetLeft+(offsetBrickX*i);
			for(int j = 0; j<numRows; j++){
				float offsety = offsetTop+(offsetBrickY*j);
				bricks.add(new Brick(con.brickimageGreen,new Vector2(offsetx, offsety), con.brickGreenStrength, con.brickFreq));
				
			}
		}
	}
	
	/**
	 * Makes a 6x6 brick field with a diamon shape
	 * @param con Constants needed for the brick generating
	 * @param bricks Constants needed for the brick generating
	 */
	private static void diamondShapeBrick(Constants con, ArrayList<Brick> bricks){
		numRows = 6;
		for(int i = 0; i<6; i++){
			float offsetx = offsetLeft+(offsetBrickX*i);
			for(int j = 0; j<numRows; j++){
				float offsety = offsetTop+(offsetBrickY*j);
				if(((i==2|i==3)&(j==0 || j == 5)) || (i==1|i==2|i==3|i==4)&(j==1||j==4)){
					bricks.add(new Brick(con.brickimageBlue,new Vector2(offsetx, offsety), con.brickBlueStrength, con.brickFreq));
				}
				if(j==2|| j==3){
					bricks.add(new Brick(con.brickimageRed,new Vector2(offsetx, offsety), con.brickRedStrength, con.brickFreq));
				}
			}
		}
		
	}
	
	/**
	 * Makes a 8x6 diamonshape within a diamondshape with green and blue bricks
	 * @param con Constants needed for the brick generating
	 * @param bricks Constants needed for the brick generating
	 * @author Marius Haugen
	 */
	private static void level3Brick(Constants con, ArrayList<Brick> bricks){
		numRows = 8;
		for(int i = 0; i<6; i++){
			float offsetx = offsetLeft+(offsetBrickX*i);
			for(int j = 0; j<numRows; j++){
				float offsety = offsetTop+(offsetBrickY*j);
				if(((j == 0 || j == 7) & (i==2 ||i ==3)) || ((j == 1 || j == 6) & (i == 1 || i == 4))){
					bricks.add(new Brick(con.brickimageGreen,new Vector2(offsetx, offsety), con.brickGreenStrength, con.brickFreq));
				}
				if((j == 2 || j == 5)& (i == 0 || i == 5)){
					bricks.add(new Brick(con.brickimageGreen,new Vector2(offsetx, offsety), con.brickGreenStrength, con.brickFreq));
				}
				if(((j == 2 || j==5) &( i== 2 || i == 3)) || ((j==3 || j == 4) & (i >0 & i<5))){
					bricks.add(new Brick(con.brickimageBlue,new Vector2(offsetx, offsety), con.brickBlueStrength, con.brickFreq));
				}
			}
		}
	}

	
}
